package com.lsh.streamgd.excelExtractor;

import arq.uparse;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.lsh.streamgd.sparql.Upload;
import com.lsh.streamgd.utils.StringUtil;
import org.apache.any23.configuration.Configuration;
import org.apache.any23.configuration.DefaultConfiguration;
import org.apache.any23.extractor.*;
import org.apache.any23.rdf.RDFUtils;
import org.apache.any23.writer.NTriplesWriter;
import org.apache.any23.writer.TripleHandler;
import org.apache.any23.writer.TripleHandlerException;
import org.apache.commons.csv.CSVParser;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.model.vocabulary.RDF;

import java.io.*;
import java.net.URISyntaxException;
import java.util.UUID;

/**
 * Created by yesonme on 16-10-23.
 */
public class Any23CsvExtractor {
    //TODO: move domain to configuration file.
    private static String domain = "http://www.szcodi.com"; //从配置文件中获取
    private static String agent = "szjmxxw_gov_cn"; //修改
    private static String lang = "zh"; //修改，数据集label的语种

    //graph info
    private static String version = "1.0";
    private static String creator = "seminfo";
    private static String title = "深圳市司法局"; //修改

    private static String file_name = "6"; //修改
    private static String class_label = "公证处名录"; //修改

    private static String path = "d:\\" + file_name + ".csv";
    private static String dest = "d:\\" + file_name + ".ttl";
    private static String owl_dest = "d:\\" + file_name + ".owl";
    private static File file = new File(path);
    private static String baseUri =  domain + "/ontology/" + agent;
    private static String classUri = baseUri + "/" + file_name;
    private static String instanceUri = domain + "/resource/" + agent;

    public static void main(String[] args) throws IOException, ExtractionException, URISyntaxException, TripleHandlerException {
        Configuration configuration = DefaultConfiguration.singleton();
        configuration.getProperties();

        ByteArrayOutputStream ba = new ByteArrayOutputStream();
        TripleHandler handler = new NTriplesWriter(ba);

        try {
            CSVExtractor csvExtractor = new CSVExtractor();
            ExtractionParameters params = new ExtractionParameters(configuration, ExtractionParameters.ValidationMode.None);
            ExtractionContext ctx = new ExtractionContext("CSV", new URIImpl(baseUri));
            ctx.setClassURI(classUri);
            ctx.setInstanceURI(instanceUri);
            ctx.setClassLabel(class_label);
            ctx.setBaseURI(baseUri);
            ctx.setVersion(version);
            ctx.setCreator(creator);
            ctx.setTitle(title);
            ctx.setLang(lang);


            Model model = ModelFactory.createDefaultModel();
            Model owlModel = ModelFactory.createDefaultModel();

            InputStream in = new BufferedInputStream(new FileInputStream(file));
            try {
//                csvExtractor.run(params, ctx, in, model, owlModel);
                //TODO: upload to virtuoso db.
                //将ontModel和model数据传入到LSH
                /**
                 *
                Upload upload = new Upload(graphDriver, graphHost, graphUsername, graphPassword);
                upload.convert2RDF(model);
                upload.convert2RDF(owlModel);
                 */

//                OutputStream f = new FileOutputStream(dest);
//                OutputStream owlf = new FileOutputStream(owl_dest);
//                model.write(f, "N3");
//                owlModel.write(owlf, "N3");
            } finally {
                in.close();
                handler.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
